//
//  HJUserAccount.swift
//  sinaWeibo
//
//  Created by hjun on 16/3/23.
//  Copyright © 2016年 hjun. All rights reserved.
//

import UIKit

class HJUserAccount: NSObject,NSCoding{
    
    
    //返回值字段	字段类型	字段说明
    
    /*
    
    "access_token": "ACCESS_TOKEN",
    "expires_in": 1234,
    "remind_in":"798114",
    "uid":"12341234"
    */
    var access_token: String?
    //用于调用access_token，接口获取授权后的access token。
    
    ///access_token的生命周期，单位是秒数。
    var expires_in:	NSTimeInterval = 0 {
        
        didSet {
            
            expiredDate = NSDate(timeIntervalSinceNow: expires_in)
            
        }
    }
    ///当前授权用户的UID。
    var uid: String?
    
    ///过期时间 转成nsdate
    
    var expiredDate: NSDate?
    
    //MARK: -用户信息
    ///用户名称
    var screen_name: String?
    
    ///用户头像
    var avatar_large: String?
    
    ///使用KVC字典转模型
    init(dict: [String: AnyObject]){
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    ///字典中的key在模型中找不到对应属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    ///对象打印
    override var description: String{
        
        get {
            let keys = ["access_token","expires_in","uid","expiredDate"]
            
            return dictionaryWithValuesForKeys(keys).description
        }
    }
    
    
    //MARK: -归档和接档
    
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token,forKey: "access_token")
        aCoder.encodeDouble(expires_in,forKey:
            "expires_in")
        aCoder.encodeObject(uid,forKey:
            "uid")
        aCoder.encodeObject(expiredDate,forKey:
            "expiredDate")
        aCoder.encodeObject(screen_name,forKey:
            "screen_name")
        aCoder.encodeObject(avatar_large,forKey: "avatar_large")
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init()
        access_token = aDecoder.decodeObjectForKey("access_token")as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid")as? String
        expiredDate = aDecoder.decodeObjectForKey("expiredDate")as? NSDate
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as?String
        screen_name =   aDecoder.decodeObjectForKey("screen_name") as?String
    }
    
}
